package demo1.demo1.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Locale;

//如果自己定制化一些功能，只要写这个组件，然后将这个组件交给springboot,springboot会帮我们装配
//拓展springmvc dispatchservlet
//如果我们要拓展springmvc,官方建议我们这样去做

@Configuration
//@EnableWebMvc //导入了一个类 DelegatingWebMvcConfiguration：从容器中获取所有WebMvc的config
public class MyMvcConfig implements WebMvcConfigurer {
//    viewResolver 实现了视图解析器接口的类，我们就可以把它看作视图解析器

    @Bean
    public ViewResolver myViewResolver(){
        return new MyViewResolver();
    }

    public static class MyViewResolver implements ViewResolver {

        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }

//    视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/random.html").setViewName("abc");
    }


//    自定义的东西就注入配置了
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginHandlerInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/","/index.html","/user/login","/addEmp","/css/**","/favicon.ico","/js/**","/img/**");
    }
}

